package com.linkage.jz.bankruptcy.fragment.Project;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.Consts;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Project.EditManagerUserInfoActivity;
import com.linkage.jz.bankruptcy.activity.Project.EditOtherUserInfoActivity;
import com.linkage.jz.bankruptcy.activity.Project.OtherUserDetailActivity;
import com.linkage.jz.bankruptcy.bean.AddressInfo;
import com.linkage.jz.bankruptcy.bean.ManagerBaseUserInfo;
import com.linkage.jz.bankruptcy.bean.ManagerUserInfo;
import com.linkage.jz.bankruptcy.bean.OtherBaseUserInfo;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.jz.bankruptcy.utils.SPUtils;
import com.linkage.seapeak.base.BaseFragment;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author FT
 * @time 2016-07-18 16:49
 */

// JUMP 
@SuppressLint("ValidFragment")
public class OtherUserBaseDetailFragment extends BaseFragment {
    RecyclerView mRecyclerView;
    private DemoAdapter mAdapter;

    private ManagerUserInfo mInfo;

    private boolean isShowNumber = false;

    OtherBaseUserInfo.UserAuth auth;

    public void setPermissionParms(OtherBaseUserInfo.UserAuth auth, boolean isShowNumber) {
        this.auth = auth;
        this.isShowNumber = isShowNumber;
        mAdapter.notifyDataSetChanged();
    }

    public static OtherUserBaseDetailFragment newInstance() {
        OtherUserBaseDetailFragment tripFragment = new OtherUserBaseDetailFragment();
        return tripFragment;
    }

    public void setInfo(ManagerUserInfo mInfo) {
        this.mInfo = mInfo;
        mAdapter.notifyDataSetChanged();
    }

    public OtherUserBaseDetailFragment() {
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        List<String> em = new ArrayList<>();
        em.add("占位的一条信息");
        initAdapter(em);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pro_detailpro_about, container, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.mRecyclerView);
        return view;
    }


    /**
     * 设置RecyclerView属性
     */
    private void initAdapter(List<String> data) {
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new DemoAdapter(getActivity(), R.layout.fragment_manager_detail_about_item, data);
        mAdapter.openLoadAnimation();
        mRecyclerView.setAdapter(mAdapter);//设置adapter
        //设置item点击事件
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.flow_edit:
                        EditOtherUserInfoActivity.start(getActivity(), mInfo);
                        break;
                    case R.id.btn_show_off:
                        hidePhone();
                        break;
                }
            }
        });
    }

    /**
     * adapter
     */
    class DemoAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
        int mLayoutId;

        public DemoAdapter(Context context, int LayoutId, List<String> str) {
            super(LayoutId, str);
            mLayoutId = LayoutId;
        }

        @Override
        public void convert(BaseViewHolder helper, String str) {
            helper.addOnClickListener(R.id.flow_edit);
            helper.addOnClickListener(R.id.btn_show_off);


            if (mInfo != null) {
                helper.setVisible(R.id.flow_edit, auth.isEditOtherMembers());
                helper.setVisible(R.id.btn_show_off, auth.isHideOtherMembersPhone());
                helper.setText(R.id.btn_show_off, isShowNumber ? "已隐藏，点击显示" : "隐藏");
                helper.setText(R.id.tv_detail_name, mInfo.getName());

                if (!TextUtils.isEmpty(mInfo.getUser().getMobile()))
                    helper.setText(R.id.tv_phone, !isShowNumber ? mInfo.getUser().getMobile() : "——");
                else
                    helper.setText(R.id.tv_phone, "");
                helper.setText(R.id.tv_email, mInfo.getUser().getEmail());
                helper.setText(R.id.tv_addr, mInfo.getAddress() == null ? "" : mInfo.getAddress().getDetailString());
            }
            if(auth !=null) {
                if (auth.isEditOtherMembers()) {

                    helper.setVisible(R.id.iv_flow_time, true);
                    helper.setVisible(R.id.flow_edit, true);
                    helper.setVisible(R.id.btn_show_off, true);
                } else {
                    helper.setVisible(R.id.iv_flow_time, false);
                    helper.setVisible(R.id.flow_edit, false);
                    helper.setVisible(R.id.btn_show_off, false);
                }
            }else {
                helper.setVisible(R.id.iv_flow_time, false);
                helper.setVisible(R.id.flow_edit, false);
                helper.setVisible(R.id.btn_show_off, false);
            }
        }
    }

    public void hidePhone() {
        if (BankruptcyApp.tokeninfo == null || BankruptcyApp.tokeninfo.getToken() == null) {
            return;
        }
        showProgress();
        Network.getAPIService()
                .OHidePhone(mInfo.getId(), getUserToken())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        try {
                            hideProgress();
                            if(json.get("code").getAsInt() == 0) {
                                ((OtherUserDetailActivity) getActivity()).getData();
//                                isShowNumber = !isShowNumber;
//                                mAdapter.notifyDataSetChanged();
//                                String jsonStr = json.getAsJsonObject("data").toString();
//                                ManagerBaseUserInfo item = new Gson().fromJson(jsonStr, ManagerBaseUserInfo.class);
//                                mInfo = item.getManagersInfo();
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        hideProgress();
                    }
                });
    }
}
